package com.learn;

import javax.swing.JPanel;
import java.awt.Font;
import java.awt.HeadlessException;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

public class SearchWindow extends JPanel {
	
	private JTextField numberField;
	private JTextField nameField;
	private JTextField clazzField;
	private JTextField majorField;
	private JTextField commuField;
	private JTextField positionField;
	private DBConnection dbc;

	/**
	 * Create the panel.
	 */
	public SearchWindow() {
		dbc = new DBConnection();
		setLayout(null);
		Font font = new Font("宋体", Font.PLAIN, 20);

		JLabel lblNewLabel = new JLabel("输入要查询信息的学号：");
		lblNewLabel.setBounds(180, 10, 220, 20);
		lblNewLabel.setFont(font);
		add(lblNewLabel);

		numberField = new JTextField();
		numberField.setBounds(400, 5, 130, 30);
		add(numberField);
		numberField.setColumns(15);
		numberField.setFont(font);

		JButton btnquery = new JButton("查询");
		btnquery.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					query();
				} catch (HeadlessException | SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnquery.setBounds(535, 5, 85, 30);
		add(btnquery);
		btnquery.setFont(font);

		JLabel lblNewLabel1 = new JLabel("姓名：");
		lblNewLabel1.setBounds(180, 50, 60, 20);
		add(lblNewLabel1);
		lblNewLabel1.setFont(font);

		nameField = new JTextField();
		nameField.setEditable(false);
		nameField.setBounds(240, 45, 380, 30);
		add(nameField);
		nameField.setFont(font);
		nameField.setColumns(10);

		JLabel lblNewLabel2 = new JLabel("班级：");
		lblNewLabel2.setBounds(180, 90, 60, 20);
		add(lblNewLabel2);
		lblNewLabel2.setFont(font);

		
		clazzField = new JTextField();
		clazzField.setEditable(false);
		clazzField.setBounds(240, 85, 380, 30);
		add(clazzField);
		clazzField.setColumns(10);
		clazzField.setFont(font);

		JLabel lblNewLabel3 = new JLabel("社团：");
		lblNewLabel3.setBounds(180, 130, 60, 20);
		add(lblNewLabel3);
		lblNewLabel3.setFont(font);

		majorField = new JTextField();
		majorField.setEditable(false);
		majorField.setBounds(240, 125, 380, 30);
		add(majorField);
		majorField.setColumns(10);
		majorField.setFont(font);

		JLabel lblNewLabel4 = new JLabel("职位：");
		lblNewLabel4.setBounds(180, 170, 60, 20);
		add(lblNewLabel4);
		lblNewLabel4.setFont(font);

		commuField = new JTextField();
		commuField.setEditable(false);
		commuField.setBounds(240, 165, 380, 30);
		add(commuField);
		commuField.setColumns(10);
		commuField.setFont(font);

		JLabel lblNewLabel5 = new JLabel("职位：");
		lblNewLabel5.setBounds(180, 210, 60, 20);
		add(lblNewLabel5);
		lblNewLabel5.setFont(font);

		positionField = new JTextField();
		positionField.setEditable(false);
		positionField.setBounds(240, 205, 380, 30);
		add(positionField);
		positionField.setColumns(10);
		positionField.setFont(font);
	}

	private void query() throws HeadlessException, SQLException {
		String number = numberField.getText();
		if (dbc.queryRecord(number)) {
			String[] values = dbc.queryInfo(number);
			nameField.setText(values[0]);
			clazzField.setText(values[1]);
			majorField.setText(values[2]);
			commuField.setText(values[3]);
			positionField.setText(values[4]);
		} else {
			JOptionPane.showMessageDialog(this, "您输入的学号不存在，请重新输入", "消息对话框", JOptionPane.WARNING_MESSAGE);
		}
	}
}
